package com.example.jobback.intercept;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.example.jobback.config.AuthConfig;
import com.example.jobback.intercept.common.CommonInterceptService;
import com.example.jobback.po.Seeker;
import com.example.jobback.util.CACHEUtil;
import com.example.jobback.util.JWTUtil;
import com.example.jobback.util.SecureEncryptionUtil;
import com.example.jobback.util.TLUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.example.jobback.util.RedisConstants.*;
import static java.time.LocalTime.now;

/**
 * @ClassName: SeekerIntercept
 * @Description:求职者拦截器
 * @Author: lhb
 * @Date: 2025/5/10
 */
@Component
@Slf4j
public class SeekerIntercept  implements HandlerInterceptor {


    private final CommonInterceptService commonInterceptService;


    private final AuthConfig authConfig;

    private final CACHEUtil cacheUtil;

    public SeekerIntercept(AuthConfig authConfig, CACHEUtil cacheUtil, CommonInterceptService commonInterceptService) {
        this.authConfig = authConfig;
        this.cacheUtil = cacheUtil;
        this.commonInterceptService = commonInterceptService;
    }

    /**
     * @Description: 请求前调用
     * @param request
     * @param response
     * @param handler
     * @return boolean
     * @Author lhb
     * @CreateTime 2025/5/10 23:01
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String token=request.getHeader("Authorization");
        //跨服务，提前验证防止response重复设置
        if(commonInterceptService.checkCrossToken(token, response)) return true;
        Seeker seeker = null;
        try {
            seeker = commonInterceptService.checkToken(token, response, authConfig.getJwtSeekerSecret(), SEEKER_KEY, Seeker.class);
        } catch (Exception e) {
            //基本不可能于response设置http状态码冲突
            throw new RuntimeException(e);
        }
        if(seeker == null) return false;
        Long userId=seeker.getUserId();
        TLUtil.set(TLUtil.USER_ID,userId);
        TLUtil.set(TLUtil.S,seeker);
        cacheUtil.expire(SEEKER_KEY+userId.toString(),SEEKER_TTL, TimeUnit.DAYS);
        log.info("求职者拦截："+seeker.getUserId().toString());
        return true;
    }


    /**
     * @Description: 请求结束后调用
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @return void
     * @Author lhb
     * @CreateTime 2025/5/10 23:01
     */

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        TLUtil.remove(TLUtil.S);
        TLUtil.remove(TLUtil.USER_ID);
    }
}
